package kef

import (
	"context"
	"encoding/json"
	"fmt"
	"gitee.com/kingeasts/keframe/request"
	"net/http"
)

type Context struct {
	context.Context
	Params []interface{}
	writer http.ResponseWriter
	request *http.Request
}

// Success 成功响应
func (t *Context) Success(result ...interface{}) {
	resp := &request.Response{}
	resp.Success = true
	if len(result) > 0 {
		resp.Result = result[0]
	}
	data, _ := json.Marshal(resp)
	_, _ = fmt.Fprintln(t.writer, string(data))
}

// Error 失败响应
func (t *Context) Error(code int, err error) {
	resp := &request.Response{}
	resp.Code = code
	resp.Error = err.Error()
	data, _ := json.Marshal(resp)
	_, _ = fmt.Fprintln(t.writer, string(data))
}
